/**
 * Hien thi bang diem cao
 */
package view;
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import model.Constant;
import model.database.PlayDB;
import model.entity.Playing;


/**
 * @author heroandtn3
 * @date Nov 5, 2012
 */
public class HighScorePanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public final static String KEY = "HighScorePanel";
	private MainPanel mainPanel;
	private JTable tbHighScore;
	private JLabel lbBack;
	private JLabel lbBg;
	private JScrollPane scrollPane;
	private DefaultTableModel tbHighScoreModel;

	/**
	 * 
	 */
	public HighScorePanel(MainPanel mainPanel) {
		// TODO Auto-generated constructor stub
		this.mainPanel = mainPanel;
		initGUI();
	}
	
	private void initGUI() {
		try {
			{
				this.setPreferredSize(new java.awt.Dimension(900, 680));
				this.setLayout(null);
				this.setBackground(new java.awt.Color(0,138,0));
				{
					scrollPane = new JScrollPane();
					scrollPane.setBackground(Color.WHITE);
					this.add(scrollPane);
					scrollPane.setBounds(371, 73, 487, 528);
					{
						tbHighScoreModel = new DefaultTableModel() {
                        	/**
							 * 
							 */
							private static final long serialVersionUID = 1L;

							@Override
                        	public boolean isCellEditable(int row, int column) {
                        		// tat cac cac o deu khong duoc edit
                        		return false;
                        	}
                        };
                        tbHighScoreModel.addColumn("No.");
                        tbHighScoreModel.addColumn("User");
                        tbHighScoreModel.addColumn("Date");
                        tbHighScoreModel.addColumn("Map");
                        tbHighScoreModel.addColumn("Level");
                        tbHighScoreModel.addColumn("Score");
						tbHighScore = new JTable();
						tbHighScore.setBackground(Color.WHITE);
						scrollPane.setViewportView(tbHighScore);
						scrollPane.getViewport().setBackground(Color.WHITE);
						tbHighScore.setModel(tbHighScoreModel);
					}
				}
				{
					lbBack = new MyLabelButton(Constant.BUTTON_DIR + "back", "Back to home", false);
					this.add(lbBack);
					lbBack.setBounds(614, 613, 48, 48);
					lbBack.addMouseListener(new MouseAdapter() {
						public void mouseClicked(MouseEvent evt) {	
							//TODO add your code for lbBack.mouseClicked
							mainPanel.switchPanel(HomePanel.KEY);
						}
					});
				}
				{
					lbBg = new JLabel(new ImageIcon(Constant.BG_DIR + "stat.png"));
					this.add(lbBg);
					lbBg.setBounds(34, 211, 256, 256);
				}
			}
		} catch(Exception e) {
			//e.printStackTrace();
		}
	} // ket thuc phuong thuc initGUI()
	
    /**
     * Tao danh sach high score 
     */
    public void initHighScore() {
    	tbHighScoreModel.setRowCount(0);
    	DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    	PlayDB pDB = new PlayDB();
    	List<Playing> plays = pDB.getTopPlay(30);
    	int no = 0;
    	for (Playing p : plays) {
    		no++;
    		String name = p.getUser().getName();
    		String day = dateFormat.format(p.getDate());
    		String map = p.getMap().getName();
    		int level = p.getLevel();
    		int score = p.getScore();
    		tbHighScoreModel.addRow(new String[] {"" + no, name, day, map, "" + level, score + ""});
    	}
    }

}
